package com.lvt.example.thinking.in.spring.bean.lifecycle;

import com.lvt.example.thinking.in.spring.ioc.overview.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

/**
 * desc：Bean 合并示例
 *
 * @author LT
 * @date 2022/4/5
 */
@Slf4j
public class BeanDefinitionMergedDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
        // 实例化
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(defaultListableBeanFactory);

        String[] locationResources = {"META-INF/dependency-lookup-context.xml","META-INF/bean-constructor-dependency-injection.xml"};
        int count = beanDefinitionReader.loadBeanDefinitions(locationResources);

        log.info("已加载的实例数量为：{}", count);
        // 根据id和类型查找
        User user = defaultListableBeanFactory.getBean("user", User.class);
        User superUser = defaultListableBeanFactory.getBean("superUser", User.class);

        // 构造器注入按照类型进行注入
        UserHolder userHolder = defaultListableBeanFactory.getBean("userHolder", UserHolder.class);
        log.info("user is:{}", user);
        log.info("superUser is:{}", superUser);
        log.info("userHolder is:{}", userHolder);
    }
}
